# app/processors/text_processor.py

import os
import csv
from typing import Dict, Any, List
from .base import BaseProcessor

class TextProcessor(BaseProcessor):
    """文本文件处理器"""
    
    def __init__(self):
        super().__init__()
        self.supported_types = ['txt', 'md', 'csv']
    
    def process(self, file_path: str, file_type: str, knowledge_base: str = "default") -> Dict[str, Any]:
        """处理文本文件"""
        from app.core.logging import get_logger
        logger = get_logger(__name__)
        
        self.validate_file(file_path, file_type)
        logger.info(f"开始处理文本文件: {file_path}, 知识库: {knowledge_base}")
        
        # 提取文本
        logger.info("提取文本内容...")
        texts = self.extract_text(file_path)
        logger.info(f"提取到 {len(texts)} 个文本段落")
        
        # 分割文本
        logger.info("分割文本...")
        text_chunks = []
        for text in texts:
            chunks = self.split_text(text)
            text_chunks.extend(chunks)
        
        logger.info(f"分割得到 {len(text_chunks)} 个文本块")
        
        # 向量化并存储
        logger.info("开始向量化存储...")
        stored_count = self.store_vectors(text_chunks, file_path, file_type, knowledge_base)
        logger.info(f"成功存储 {stored_count} 个向量到知识库 {knowledge_base}")
        
        return {
            'text_chunks_count': len(text_chunks),
            'images_count': 0,
            'text_chunks': text_chunks,
            'images': [],
            'stored_vectors': stored_count,
            'knowledge_base': knowledge_base
        }
    
    def extract_text(self, file_path: str) -> List[str]:
        """提取文本内容"""
        file_type = os.path.splitext(file_path)[1].lower()
        
        if file_type == '.csv':
            return self._extract_csv_text(file_path)
        else:
            return self._extract_plain_text(file_path)
    
    def extract_images(self, file_path: str) -> List[str]:
        """文本文件不包含图片"""
        return []
    
    def _extract_plain_text(self, file_path: str) -> List[str]:
        """提取纯文本内容"""
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read()
                return [content] if content.strip() else []
        except UnicodeDecodeError:
            # 尝试其他编码
            try:
                with open(file_path, 'r', encoding='gbk') as f:
                    content = f.read()
                    return [content] if content.strip() else []
            except:
                return []
    
    def _extract_csv_text(self, file_path: str) -> List[str]:
        """提取CSV文本内容"""
        texts = []
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                reader = csv.reader(f)
                for row in reader:
                    if row:
                        texts.append(', '.join(row))
        except UnicodeDecodeError:
            try:
                with open(file_path, 'r', encoding='gbk') as f:
                    reader = csv.reader(f)
                    for row in reader:
                        if row:
                            texts.append(', '.join(row))
            except:
                pass
        
        return texts 